package praca.framework;

import praca.interfejsy.FileIO;
import praca.interfejsy.Game;
import praca.interfejsy.Graphics;
import praca.interfejsy.Input;
import praca.interfejsy.Music;
import praca.interfejsy.Screen;
import android.app.Activity;
import android.content.res.AssetManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public abstract class GameGL extends Activity implements Game{

	public Screen currentScreen;
	private Graphics g;
	private Input input;
	private Music music;
	private FileIO io;
	private GLSurfaceView view;
	private GameRenderer renderer;
	private AssetManager assets;
	public float width;
	public float height;
	public boolean isSoundEnabled = true;
	
	public static enum GameState{
		INITIALISATION, RUNNING, PAUSED, FINISHING, IDLE
	};
	public GameState gameState = GameState.INITIALISATION;

	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		assets = this.getAssets();
		io = new FileIOGL(this);
		g = new GraphicsGL(this);
		input = new InputGL();
		

		//ustawiamy okna na pelny ekran
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
		
        
        view = new GLSurfaceView(this); //nowy widok wymaga Context (this)
        
        
        renderer = new GameRenderer(this);
        
        view.setOnTouchListener(((InputGL)input).listener);
        view.setRenderer(renderer);
        
        setContentView(view);
       
	}
	
	public float getWidth(){
		return width;
	}
	
	public float getHeight(){
		return height;
	}
	
	@Override
	public Input getInput(){
		return input;
	}
	
	public void setGameState(GameState state){
		gameState = state;
	}
	
	@Override
	public void onResume(){
		
		
		super.onResume();
		view.onResume();
	}
	
	@Override
	public void onPause(){
		gameState = GameState.PAUSED;
		currentScreen.pause();
		if(isFinishing()){
			currentScreen.dispose();
		}
		
		super.onPause();
		view.onPause();
		Log.d("touch", "onPause wywolanie");
	}
	
	
	@Override
	public Screen getCurrentScreen() {
		return currentScreen;
	}
	

	@Override
	public abstract Screen getStartScreen();

	@Override
	public void setCurrentScreen(Screen sc) {
		currentScreen.dispose();
		
		currentScreen = sc;
		
		((GraphicsGL)g).enableStateTexture();
		g.clearTransformations();
		currentScreen.resume();
		
	}



	
	@Override
	public Graphics getGraphics(){
		return  g;
	}
	
	@Override
	public AssetManager getAssetManager(){
		return assets;
	}
	
	@Override 
	public GLSurfaceView getView(){
		return view;
	}
	
	@Override 
	public FileIO getFileIO(){
		return io;
	}
	
	@Override
	public void setSoundsEnabled(boolean wlaczone){
		isSoundEnabled = wlaczone;
		if(isSoundEnabled)
			getFileIO().saveSettings("sounds=on");	
		else
			getFileIO().saveSettings("sounds=off");
	}
	
	public boolean isSoundEnabled(){
		return isSoundEnabled;
	}
	
	@Override
	public void onBackPressed(){
		super.onBackPressed();
	}
	
}
